// pages/benchPages/data95/data95.js
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum:1,
    activeTab:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNowDate();
    this.setData({
      scrollHeight: app.getScrollHeight(125)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toggleTab: function (e) {
    let activeTab = e.currentTarget.dataset.activenum
    this.setData({
      activeTab: activeTab
    })
    if(!this.data.list_95){
      return
    }
    this.setList()
  },
  nowDateChange(e){
    console.log(e);
    this.setData({
      nowDate: e.detail.value
    })
    if (this.data.inputCode) {
      this.data.list_95=[]
      this.data.pageNum=1
      this.get95data()
    }
  },
  agoDateChange(e) {
    console.log(e);
    this.setData({
      agoDate: e.detail.value
    })
    if(this.data.inputCode){
      this.data.list_95 = []
      this.data.pageNum = 1
      this.get95data()
    }
  },
  inputChange: function (e) {
    let inputCode = e.detail.value
    this.setData({
      inputCode: inputCode
    })
  },
  search(){
    if (!this.data.inputCode) {
      wx.showToast({
        title: '请输入桩号！',
        image: '/images/gantan.png'
      })
      return
    }
    this.data.list_95=[]
    this.data.pageNum = 1
    this.get95data()
  },
  get95data(){
    let list_95 = this.data.list_95 || []
    lbbService.get95data({ hid: this.data.inputCode, agoDate: this.data.agoDate, nowDate: this.data.nowDate, pageNum:this.data.pageNum}, (res)=> {
      console.log(res);
      if (res.data.length == 0 && this.data.pageNum>1){
        wx.showToast({
          title: '无更多数据',
          image:'/images/gantan.png'
        })
        return
      }
      this.setData({
        list_95: list_95.concat(res.data)
      })
      let rentList = this.data.list_95.filter(item => item.leaseStat != 0);
      let backList = this.data.list_95.filter(item => item.leaseStat == 0);
      this.setData({
        rentList: rentList,
        backList: backList
      })
      this.setList()
    })
  },
  setList(){
    if( this.data.activeTab==0){
      this.setData({
        show_list:this.data.list_95
      })
    } else if (this.data.activeTab == 1) {
      this.setData({
        show_list: this.data.backList
      })
    } else if (this.data.activeTab == 2) {
      this.setData({
        show_list: this.data.rentList
      })
    }
  },
  getMoreData(){
    this.data.pageNum+=1
    if (!this.data.inputCode) {
      wx.showToast({
        title: '请输入桩号！',
        image: '/images/gantan.png'
      })
      return
    }
    this.get95data()
  },
  getNowDate: function () {
    let reg = /\//g
    let nowTime = new Date()
    let agoTime = new Date(nowTime.getTime()-1000*60*60*24*6)
    let nowDate = util.formatTime(nowTime).substr(0, 10).replace(reg, '-')
    let agoDate = util.formatTime(agoTime).substr(0, 10).replace(reg, '-')
    this.setData({
      nowDate: nowDate,
      date: nowDate,
      agoDate: agoDate
    })
  },
})
